package com.tech.platform.cms.admin.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tech.platform.cms.admin.dao.mapper.SubSystemMapper;
import com.tech.platform.cms.admin.dao.pojo.SubSystem;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import com.tech.platform.cms.common.model.PageResult;
import com.tech.platform.cms.common.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SubSystemService {

    @Autowired
    private SubSystemMapper subSystemMapper;

    public CommonResult<String> add(SubSystem system){

        if(ValidationUtil.validateBean(system).hasErrors()){
            return new CommonResult<>(Constant.PARAM_ERROR,ValidationUtil.validateBean(system).getErrors());
        }

        system.setSecretKey(StrUtil.uuid());
        system.setAddTime(new Date());
        system.setModifyTime(new Date());
        subSystemMapper.insert(system);
        return new CommonResult<>(Constant.SUCCESS,"success");
    }

    public CommonResult<String> update(SubSystem system){
        if(ValidationUtil.validateBean(system).hasErrors()){
            return new CommonResult<>(Constant.PARAM_ERROR,ValidationUtil.validateBean(system).getErrors());
        }

        if(system.getId()==0){
            return new CommonResult<>(Constant.PARAM_ERROR,ValidationUtil.validateBean(system).getErrors());
        }

        system.setModifyTime(new Date());
        subSystemMapper.updateById(system);
        return new CommonResult<>(Constant.SUCCESS,"success");
    }

    public CommonResult<String> delete(int id){
        subSystemMapper.deleteById(id);
        return new CommonResult<>(Constant.SUCCESS,"success");
    }

    public PageResult<List<SubSystem>> page(int current, int size, String keyWord){
        Page<SubSystem> page = new Page<>(current,size);
        QueryWrapper<SubSystem> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyWord);
        subSystemMapper.selectPage(page,wrapper);
        return new PageResult<>(Constant.SUCCESS,"success",page.getTotal(),page.getRecords());
    }
}
